package com.hailong.zyt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hailong.zyt.beans.Student;
import com.hailong.zyt.mapper.MajorMapper;
import com.hailong.zyt.service.StudentService;
import com.hailong.zyt.service.impl.StudentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hailong
 * @since 2021-05-13
 */
@Slf4j
@Controller
public class StudentController {

    private StudentService studentService;

    @Autowired
    private MajorMapper majorMapper;

    /*start--管理员相关操作---------------------------------------------------------------------------------------------*/

    /*
    * 来到学生管理界面
    * */
    @GetMapping("/admin/students")
    public String students(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        Page<Student> studentPage = new Page<>(pn, 8);
        IPage<Student> page = studentService.page(studentPage);
        model.addAttribute("students", page);
        return "/admin/admStudent";
    }

    /*
    * 学生详细信息
    * */
    @GetMapping("/admin/student/{stuId}")
    public String toEditStudent(@PathVariable("stuId") Long stuId, Model model) {
        Student student = studentService.getById(stuId);
        model.addAttribute("majors", majorMapper.selectList(null));
        model.addAttribute("student", student);
        return "/admin/editStudent";
    }
    //修改学生信息
    @PutMapping("/admin/student")
    public String editStudent(Student student) {
        studentService.updateById(student);
        return "redirect:/admin/students";
    }
    //添加学生
    @PostMapping("/admin/addStudent")
    public String addStudent(Student newStudent, Model model) {
        boolean saved = studentService.addStudent(newStudent, model);
        return "redirect:/admin/students";
    }
    //删除学生
    @DeleteMapping("/admin/deleteStudent/{stuId}")
    public String deleteStudent(@PathVariable("stuId") Long stuId) {
        boolean delete = studentService.removeById(stuId);
        return "redirect:/admin/students";
    }

    /*管理员相关操作-------------------------------------------------------------------------------------------------end*/


    /*start--学生相关操作-----------------------------------------------------------------------------------------------*/

    /**
     * 学生登录
     * */
    @GetMapping("/index/stuLogin")
    public String toStuLogin(){
        return "/index/studentLogin";
    }

    @PostMapping("/index/stuLogin")
    public String stuLogin(Student user, HttpSession session, Model model) {
        Student student = studentService.getById(user);
        if(student == null) {
            model.addAttribute("em_id", "用户不存在");
            return "/index/studentLogin";
        }
        else if(!student.getStuPassword().equals(user.getStuPassword())){
            model.addAttribute("em_pwd", "密码不正确");
            return "/index/studentLogin";
        }
        else {
            session.setAttribute("student", student);
            session.setAttribute("userType", "student");
            return "redirect:/student/main";
        }
    }

    /**
     * 退出登录
     * */
    @GetMapping("/student/loginOut")
    public String loginOut(HttpSession session) {
        session.removeAttribute("student");
        return "redirect:/";
    }

    /*
     * 学生注册
     * */
    @GetMapping("/index/register")
    public String toStuRegister(Model model){
        model.addAttribute("majors", majorMapper.selectList(null));
        return "/index/register";
    }

    @PostMapping("/index/register")
    public String StuRegister(Student newStudent, Model model){
        log.info("学生：{}", newStudent);
        try {
            boolean sv = studentService.save(newStudent);
            return "/index/studentLogin";
        }
        catch (Exception e){
            //注册失败
            model.addAttribute("em_id","用户已存在");
            return "/index/register";
        }
    }


    @GetMapping("/student/main")
    public String toMain(Model model, HttpSession session){
        Student student = (Student) session.getAttribute("student");
        //查询选了几门课
        model.addAttribute("course_count", studentService.getCourseCount(student.getStuId()));
        //查询加入了几个小组
        model.addAttribute("group_count", studentService.getGroupCount(student.getStuId()));
        model.addAttribute("finish_total", studentService.getHWFinishedRate(student.getStuId()));
        return "/student/stuMain";
    }

    @GetMapping("/student/info")
    public String toInfo(Model model, HttpSession session){
        Student user = (Student) session.getAttribute("student");
        Student student = studentService.getById(user.getStuId());
        model.addAttribute("student", student);
        return "/student/stuInfo";
    }
    @PutMapping("/student/changePassword")
    public String changePassword(Student student, String newPassword, String reNewPassword, Model model, HttpSession session) {
        boolean flag = studentService.changePassword(student, newPassword, reNewPassword, model);
        if(flag) {
            session.setAttribute("student", studentService.getById(student.getStuId()));
            return "redirect:/student/info";
        }
        else {
            return "/student/stuInfo";
        }
    }
    /*学生相关操作---------------------------------------------------------------------------------------------------end*/



    @GetMapping("/student/group")
    public String toGroup(){
        return "/student/group/stuGroup";
    }




    @Autowired
    public void setStudentService(StudentServiceImpl studentService) {
        this.studentService = studentService;
    }
}

